<script>
import { resetPasswordAPI, deleteEmployeeAPI } from '@/api/system/employee'
import { mapGetters } from 'vuex'
import EmployeeForm from '@/views/system/employee/components/employeeForm/employeeForm.vue'

export default {
  name: 'employee',
  components: { EmployeeForm },
  data() {
    return {
      pageParams: {
        name: undefined,
        page: 1,
        pageSize: 15
      },
      dialogVisible: false,
      edit: false
    }
  },
  methods: {
    /* 获取员工列表的回调函数 */
    async fetchEmployeeList() {
      try {
        await this.$store.dispatch('employee/fetchEmployeeList', this.pageParams)
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    handleSizeChange(pageSize) {
      this.pageParams.pageSize = pageSize
      this.fetchEmployeeList()
    },
    handleCurrentChange(page) {
      this.pageParams.page = page
      this.fetchEmployeeList()
    },
    async handleReset(id) {
      try {
        const { msg } = await resetPasswordAPI(id)
        this.$message.success(msg)
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    async handleDelete(id) {
      try {
        const { msg } = await deleteEmployeeAPI(id)
        await this.fetchEmployeeList()
        this.$message.success(msg)
      } catch (error) {
        this.$message.error(error.message)

      }
    },
    handleAdd() {
      this.edit = false
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.edit = true
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.employeeForm.findRole(row)
      })
    },
    addSuccess() {
      this.dialogVisible = false
      this.fetchEmployeeList()
    },
    handleSearch(){
      this.pageParams.page=1
      this.fetchEmployeeList()
    }
  },
  computed: {
    ...mapGetters('employee', ['employeeList', 'total']),
    title() {
      return this.edit ? '编辑员工' : '添加员工'
    }
  },
  created() {
    this.fetchEmployeeList()
  }
}
</script>

<template>
  <div class="employee">
    <el-card>
      <el-form inline>
        <el-form-item label="员工姓名">
          <el-input placeholder="请输入员工姓名" size="small" v-model="pageParams.name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-button size="small" type="success" icon="el-icon-plus" @click="handleAdd">添加员工</el-button>
      <el-table border stripe :header-cell-style="{background:'#4d8ed9',color:'#fff'}" size="small" :data="employeeList">
        <el-table-column label="序号" type="index" align="center"></el-table-column>
        <el-table-column label="员工姓名" prop="name" align="center"></el-table-column>
        <el-table-column label="登录账号" prop="userName" align="center"></el-table-column>
        <el-table-column label="联系方式" prop="phonenumber" align="center"></el-table-column>
        <el-table-column label="角色" prop="roleName" align="center"></el-table-column>
        <el-table-column label="状态" align="center">
          <template #default="{row}">
            <el-tag>{{ row.status === 1 ? '启用' : '停用' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="createTime" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <el-button type="text" @click="handleEdit(row)">编辑</el-button>
            <el-button type="text" @click="handleDelete(row.id)">删除</el-button>
            <el-button type="text" @click="handleReset(row.id)">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageParams.page" :page-sizes="[15, 20, 30, 40]" :page-size="pageParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" background></el-pagination>
    </el-card>

    <el-dialog :title="title" :visible.sync="dialogVisible" width="35%" @close="$refs.employeeForm.resetForm()">
      <employee-form :edit="edit" ref="employeeForm" @addSuccess="addSuccess"></employee-form>
    </el-dialog>

  </div>
</template>

<style scoped lang="scss">
.employee {
  .el-card {
    margin-bottom: 10px;

    .el-form-item {
      margin-bottom: unset;
    }

    .el-table {
      margin: 10px 0;
    }

    .el-pagination {
      display: flex;
      justify-content: center;
    }
  }
}
</style>
